package com.njtech.pet.dao.example;

import com.njtech.pet.constant.Constant;
import com.njtech.pet.dao.PowerDao;
import com.njtech.pet.entity.Power;
import com.njtech.pet.entity.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.security.cert.LDAPCertStoreParameters;
import java.util.List;
import java.util.Scanner;

/**
 * @author lxj
 * Date 2020-03-13 16:38
 * Description 测试PowerDao
 */
public class PowerDaoExample {
    static PowerDao powerDao;
    static Scanner scanner;

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-dao.xml");
        powerDao = (PowerDao) context.getBean("powerDao");

        System.out.println("准备开始测试" + powerDao.toString());

        scanner = new Scanner(System.in);

        //start flag -1 end flag 0
        int num = -1;
        System.out.println("please enter test index:");
        System.out.println("0: exit,");
        System.out.println("1: test insert,");
        System.out.println("2: test update,");
        System.out.println("3: test select.");
        while (num != 0) {
            num = scanner.nextInt();
            switch (num) {
                case 0:
                    System.out.println("bye.");break;
                case 1:
                    insert();
                    break;
                case 2:
                    update();
                    break;
                case 3:
                    select();
                    break;
                default:
                    System.err.println("undefined index, please enter again.");
            }
            System.out.println("waiting for your command: ");
        }
    }

    private static void insert() {
        Power power = new Power();
        System.out.println("please enter power:");
        String a = scanner.next();
        power.setPower(a);
        power.setState(Constant.Power.STATE_VALID.getValue());
        User user = new User();
        System.out.println("please enter user id:");
        int id = scanner.nextInt();
        user.setId(id);
        power.setUser(user);
        System.out.println("inserting...");
        powerDao.insert(power);
        System.out.println("insert finished please check database");
    }

    private static void update() {
        Power power = new Power();
        power.setState(Constant.Power.STATE_INVALID.getValue());
        int id;
        System.out.println("please enter power id:");
        id = scanner.nextInt();
        power.setId(id);
        System.out.println("updating...");
        powerDao.updateByPowerId(power);
        System.out.println("update finished please check database");

    }

    private static void select() {
        User user = new User();
        System.out.println("please enter user id:");
        int id = scanner.nextInt();
        user.setId(id);

        System.out.println("test select with all state");
        List<Power> powers = powerDao.selectByUserIdAndPowerState(user, null);
        for (Power power:powers) {
            System.out.println(power);
        }
        System.out.println("test select with state valid");
        powers = powerDao.selectByUserIdAndPowerState(user, Constant.Power.STATE_VALID.getValue());
        for (Power power:powers) {
            System.out.println(power);
        }
        System.out.println("test select with state invalid");
        powers = powerDao.selectByUserIdAndPowerState(user, Constant.Power.STATE_INVALID.getValue());
        for (Power power:powers) {
            System.out.println(power);
        }
    }
}
